package com.spark.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 默认在head里增加Accept-Language:en-US就可以切换，注意是中线而不是下划线
 * 还可以自定义从请求参数里来控制语言，我觉得默认这种方式比较好，
 */
@Controller
@SpringBootApplication
public class I18nApplication {

	@Autowired
	private MessageSource ms;

	public static void main(String[] args) {
		SpringApplication.run(I18nApplication.class, args);
	}

	@PostMapping
	@ResponseBody
	public String index(HttpServletRequest request){
		System.out.println(LocaleContextHolder.getLocale());
		return ms.getMessage("hello",null, LocaleContextHolder.getLocale());
	}

	@GetMapping
	public String index(){
		return "index";
	}
}
